package com.glory.brepda.adapter;

import android.os.Looper;
import android.widget.CheckBox;


import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.entity.wbChangeEqpEntity;

import java.util.List;

public class wbChangeEqpAdapter extends BaseQuickAdapter<wbChangeEqpEntity, BaseViewHolder> {
    public wbChangeEqpAdapter(@Nullable List<wbChangeEqpEntity> data) {
        super(R.layout.adapter_wb_change_eqp, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, wbChangeEqpEntity entity) {
        CheckBox checkBox = helper.getView(R.id.isSelected);
        helper.setText(R.id.tvEqpId, entity.getEqpList());

        // 移除旧的监听器，避免重复绑定
        checkBox.setOnCheckedChangeListener(null);

        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            // 防止由于代码设置 checked 状态而触发监听器
            if (checkBox.isPressed() || buttonView.isPressed()) {
                if (isChecked) {
                    // 1. 取消之前选中的项
                    for (int i = 0; i < getData().size(); i++) {
                        getData().get(i).setSelected(false);
                    }
                    // 2. 设置当前项为选中
                    entity.setSelected(true);

                    // 延迟更新，避免布局计算冲突
                    new android.os.Handler(Looper.getMainLooper()).postDelayed(() -> {
                        notifyDataSetChanged();
                    }, 50);
                } else {
                    entity.setSelected(false);
                }
            }
        });

        checkBox.setChecked(entity.isSelected());
    }


}
